package getui

import (
	"data/config"
	"public/mycache"
	"public/mysqldb"
	"time"
)

//绑定个推的clientid
func OnBindGetui(username, clientid string) bool {
	//先从缓存中获取
	cache := mycache.OnGetCache("getuitbl")
	var tp interface{}
	var b = false
	tp, b = cache.Value(username)

	if b {
		tmp := tp.(string)
		if tmp == clientid { //无更新
			return true
		}
	}

	//重数据库里面获取
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var info Getui_bind_tbl
	orm.Where("getui_client_id = ?", clientid).Find(&info)
	if username != info.Id_card {
		if len(info.Id_card) > 0 {
			cache.Delete(info.Id_card)
		}
		info.Id_card = username
		info.Getui_client_id = clientid
		orm.Save(&info)
	}

	//保存缓存
	cache.Add(username, clientid, 24*time.Hour)
	//------------------end
	return true
	//------------------end
}

//通过身份证id获取用户个推的clientid列表   为空 表示未找到
func OnGetClientID(username string) []string {
	//先从缓存中获取
	cache := mycache.OnGetCache("getuitbl")
	var tp interface{}
	var b = false
	tp, b = cache.Value(username)

	if b {
		return tp.([]string)
	}

	//重数据库里面获取
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var infos []Getui_bind_tbl
	orm.Where("id_card = ?", username).Find(&infos)
	var strs []string
	for _, v := range infos {
		strs = append(strs, v.Getui_client_id)
	}

	//保存缓存
	if len(strs) > 0 {
		cache.Add(username, strs, 24*time.Hour)
	}
	//------------------end
	return strs
}
